import request from '@/utils/request'
import { toolConvey } from '../convey'
import { toolSync } from '../sync'

const envapi = process.env.VUE_APP_ADV_OCEAN_API
// 广告组列表
export function getCampaignList (data) {
  const params = {
    filtering: [],
    order_by: data.order_by,
    order_type: data.order_type,
    page: data.page,
    page_size: data.page_size
  }
  if (data.keyWord) {
    if( parseInt(data.keyWord) == data.keyWord){
      params.filtering.push({
        field: 'id',
        operator: 'LIKE',
        value: data.keyWord
      })
    }else {
      params.filtering.push({
        field: 'name',
        operator: 'LIKE',
        value: data.keyWord
      })
    }
  }
  if (data.account_id) {
    params.filtering.push({
      field: 'account_id',
      operator: 'EQUALS',
      value: data.account_id
    })
  }
  if ( data.status  ) {
    if( data.status.length > 0 ){
      params.filtering.push({
        field: 'status',
        operator: 'IN',
        value: data.status
      })
    }
  }
  if (data.landing_type) {
    params.filtering.push({
      field: 'landing_type',
      operator: 'EQUALS',
      value: data.landing_type
    })
  }
  if (data.budget_mode) {
    params.filtering.push({
      field: 'budget_mode',
      operator: 'EQUALS',
      value: data.budget_mode
    })
  }
  if (data.created_at && data.created_at.length > 0) {
    params.filtering.push({
      field: 'created_at',
      operator: 'GREATER_EQUALS',
      value: data.created_at[0]
    })
    params.filtering.push({
      field: 'created_at',
      operator: 'LESS_EQUALS',
      value: data.created_at[1]
    })
  }
  return request({
    url: envapi +  '/admin/ocean/campaign/select',
    method:'post',
    data: params
  })
}

// 广告组(无分页)
export function getCampaignListGet (data) {
  const params = {
    filtering: []
  }
  if (data.landing_type) {
    params.filtering.push({
      field: 'landing_type',
      operator: 'EQUALS',
      value: data.landing_type
    })
  }
  if (data.account_id) {
    params.filtering.push({
      field: 'account_id',
      operator: 'EQUALS',
      value: data.account_id
    })
  }
  if (data.status) {
    params.filtering.push({
      field: 'status',
      operator: 'EQUALS',
      value: data.status
    })
  }
  return request({
    url: envapi +  '/admin/ocean/campaign/get',
    method:'post',
    data: params
  })
}

// 广告组详情
export function getCampaignDetails (id) {
  return request({
    url: envapi + '/admin/ocean/campaign/read',
    method:'post',
    data: { id:id }
  })
}
// 广告组状态(启用、暂停)
export function updateStatus (account_id,param) {
  return toolConvey(account_id,'2/campaign/update/status/',param,'post')
}

// 创建广告组
export function campaignAdd (account_id,param) {
  if(!param.campaign_name){
    param.campaign_name = param.name
  }
  return toolConvey(account_id,'2/campaign/create/',param,'post')
}

// 广告组同步
export function campaignSync (account_ids,type) {
  return toolSync(account_ids,'CAMPAIGN')
}
